package cn.funeralobjects.youtube.dl.extension.controller;

import cn.funeralobjects.youtube.dl.extension.controller.model.DownloadTaskVo;
import cn.funeralobjects.youtube.dl.extension.controller.model.DownloadingStatusVo;
import cn.funeralobjects.youtube.dl.extension.controller.model.TaskModifyVo;
import cn.funeralobjects.youtube.dl.extension.converter.DownloadTaskConverter;
import cn.funeralobjects.youtube.dl.extension.converter.DownloadingStatusConverter;
import cn.funeralobjects.youtube.dl.extension.service.DownloadService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author FuneralObjects
 * Create date: 2020/4/4 2:20 PM
 */
@RestController
@RequestMapping("/task")
public class TaskController {

    @Resource
    private DownloadService downloadService;

    @Resource
    private DownloadTaskConverter downloadTaskConverter;

    @Resource
    private DownloadingStatusConverter downloadingStatusConverter;

    @GetMapping("/completed")
    public List<DownloadTaskVo> getAllCompleted(){
        return downloadService.getCompleted().parallelStream().map(downloadTaskConverter::toVo).collect(Collectors.toList());
    }

    @GetMapping("/downloading-status")
    public DownloadingStatusVo getDownloadingStatus() {
        return downloadingStatusConverter.toVo(downloadService.getDownloadingStatus());
    }

    @PostMapping
    public void addTask(@RequestBody TaskModifyVo task) {
        downloadService.addDownloadTask(task.getId(), task.getFormat(), task.getTitle(), task.getProxy());
    }

    @PutMapping("/retry/{id}/{format}")
    public void retry(@PathVariable String id, @PathVariable String format) {
        downloadService.retryInterrupt(id, format);
    }

    @PutMapping("/stop/{id}/{format}")
    public void stop(@PathVariable String id, @PathVariable String format) {
        downloadService.stop(id, format);
    }

    @DeleteMapping("/interrupt/{id}/{format}")
    public void remove(@PathVariable String id, @PathVariable String format) {
        downloadService.removeInterrupt(id, format);
    }

    @GetMapping("/log/now/{id}/{format}")
    public String getNowLog(@PathVariable String id, @PathVariable String format) {
        return downloadService.getCurrentLog(id, format);
    }
}
